<?php

class Default_HospitalController extends ZF_DefaultController {

    public function detailAction() {
        if ($this->getRequest()->isGet()) {
            $id_hospital = $this->getParam('id');
            $model = new Default_Model_HospitalDetail();
            $data = $model->getById($id_hospital);

            $this->view->hospital = $data;

            //hot_comment
            $model_rate = new Default_Model_Rate();
            $id_item = $model_rate->getPrefixHospital() . '_' . $id_hospital;
            $data_rate = $model_rate->getByItemId($id_item);

            $overall = 0;

            foreach ($data_rate as $row) {
                $overall += $row['overall'];
            }

            $overrall = $overall / count($data_rate);
            $this->view->total_review = count($data_rate);
            $this->view->overall = $overrall;
            $this->view->id_item = $id_item;

            $model_user = new Default_Model_User();
            $data_user = $model_user->getByuserId($data_rate[0]['id_user']);

            $this->view->displayname = $data_user['displayname'];
            $this->view->comment = $data_rate[0]['comment'];

            //rating
            $profileNameSpace = $this->view->profileNameSpace;
            if ($profileNameSpace) {
                $id_user = $profileNameSpace['id'];
                $data_user_rate = $model_rate->getByItemIdUserId($id_item, $id_user);
                if (!$data_user_rate)
                    $this->view->isRating = false;
                else {
                    $this->view->isRating = true;
                    $this->view->rating = $data_user_rate;
                }
                $this->view->itemName = "hospital";
                $this->view->position = "position: absolute; top: 400px; left: 50px;";
            }
        }
    }

    public function listAction() {
        $model = new Admin_Model_HospitalDetail();
        $paginator = Zend_Paginator::factory($model->listall());
        $paginator->setItemCountPerPage(20);
        $paginator->setPageRange(3);
        $currentPage = $this->_request->getParam('page', 1);
        $paginator->setCurrentPageNumber($currentPage);
        $this->view->hospitals = $paginator;
    }

    public function ratingAction() {
        $profileNameSpace = $this->view->profileNameSpace;
        if ($profileNameSpace) {
            $type1 = $this->getParam('type1');
            $type2 = $this->getParam('type2');
            $type3 = $this->getParam('type3');
            $id_item = $this->getParam('id_item');
            $comment = $this->getParam('comment');
            $isRating = $this->getParam('isRating');
            $model_rate = new Default_Model_Rate();
            if (!$isRating) {
                if ($model_rate->insert($id_item, $profileNameSpace['id'], $type1, $type2, $type3, $comment) != -1) {
                    $data = array('error' => '0');
                    $this->_helper->json->sendJson($data);
                } else {
                    $data = array('error' => '1');
                    $this->_helper->json->sendJson($data);
                }
            } else {
                $model_rate->setIdItem($id_item);
                $model_rate->setIdUser($profileNameSpace['id']);
                $model_rate->setType1($type1);
                $model_rate->setType2($type2);
                $model_rate->setType3($type3);
                $model_rate->setComment($comment);
                if ($model_rate->update() != -1) {
                    $data = array('error' => '0');
                    $this->_helper->json->sendJson($data);
                } else {
                    $data = array('error' => '1');
                    $this->_helper->json->sendJson($data);
                }
            }
        }
    }
}